package com.share.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

/*
一、HttpServletRequest 也是一个域对象
    1、getAttribute()、setAttribute()
 */
public class demo1_info extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        System.out.println("=======================tomcat提供===========================");
        System.out.println("servlet: " + this.getClass().getName());
        System.out.println("servletContext: " + getServletContext().getClass().getName());
        System.out.println("req: " + req.getClass().getName());
        System.out.println("resp: " + resp.getClass().getName());
        System.out.println("=======================请求行===========================");
        System.out.println("demo1-contextPath: " + req.getContextPath());
        System.out.println("demo1-requestURL: " + req.getRequestURL());
        System.out.println("demo1-requestURI: " + req.getRequestURI());
        System.out.println("demo1-remoteAddr: " + req.getRemoteAddr());
        System.out.println("demo1-remoteHost: " + req.getRemoteHost());
        System.out.println("demo1-remotePort: " + req.getRemotePort());
        System.out.println("=======================header===========================");
        //post
//        req.setCharacterEncoding("utf-8");
//        System.out.println("1111111111:" + req.getParameter("name"));

        //get
//        System.out.println(new String(req.getParameter("name").getBytes("iso-8859-1"),"utf-8"));

        final Enumeration names = req.getHeaderNames();
        while(names.hasMoreElements()){
            String value = (String)names.nextElement();
            System.out.println("demo1-header: " + value + "----->" + req.getHeader(value));
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }
}
